﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Data : Form
    {
        public Data()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Data_Load(object sender, EventArgs e)
        {
            var cmdString = "select *from Articles";

            var table = DbHeper.GetDataTable (cmdString);

            dataGridView1.DataSource = table; 
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }


        //查找
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Title like '%{0}'", name);

            var dataTable =DbHeper.GetDataTable (sql);

            dataGridView1.DataSource = dataTable ;
       
        }


        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            Editfrom editfrom = new Editfrom();
           var res= editfrom.ShowDialog ();

            if(res==DialogResult.Yes)
            {
                var name = textBox1.Text;
                var cmdString = string.Format("select *from Articles where Title like '{0}'", name);

                var table = DbHeper.GetDataTable(cmdString);

                dataGridView1.DataSource = table;
            }
            else
            {
                MessageBox.Show("no");
            }
        }


        //更新
        private void button3_Click(object sender, EventArgs e)
        {

            if (dataGridView1 .SelectedRows .Count > 0)
            {
                 var id = (int )dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string )dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author =(string ) dataGridView1.SelectedRows[0].Cells["Author"].Value;
                 var createdTime= (string )dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;

            Editfrom editfrom = new Editfrom(id ,title ,author ,createdTime );
         var res= editfrom.ShowDialog ();

                if (res == DialogResult.Yes)
                {
                    var name = textBox1.Text;
                    var cmdString = string.Format("select *from Articles where Title like '%{0}'", name);

                    var table = DbHeper.GetDataTable(cmdString);

                    dataGridView1.DataSource = table;
                }
                else
                {
                    MessageBox.Show("no");
                }
            }
            else
            {
                MessageBox.Show("您未选择数据", "温馨提示");
            }
   
        }


        //删除
        private void button4_Click(object sender, EventArgs e)
        {

            if (dataGridView1 .SelectedRows .Count > 0)
            {
                  var row  = dataGridView1.SelectedRows[0];

                 var id = (int )row.Cells["Id"].Value;

                 var res= MessageBox.Show("是否删除", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                 if (res.Equals (DialogResult .Yes ) )
                    {
                     var sql = string.Format("delete from Articles where Id={0}", id );
                    var rowCount = DbHeper.AddOrUpdataDelete(sql);
                    if (rowCount >0)
                    {
                        var name = textBox1.Text;
                        var cmdString = string .Format ("select *from Articles where Title like '{0}'",name) ;

                        var table = DbHeper.GetDataTable(cmdString);

                        dataGridView1.DataSource = table;
                        MessageBox.Show("删除成功", "温馨提示");
                    }
                    }

            }
            else
            {
                MessageBox.Show("您未选择数据", "温馨提示");
            }


        
        }
    }
}
